package com.dqyt.app.service;

import com.alibaba.fastjson.JSON;
import com.dqyt.app.bean.WorkBean;
import com.dqyt.common.check.CheckTool;
import com.dqyt.common.constant.AuthorityID;
import com.dqyt.common.constant.Opt;
import com.dqyt.common.utils.UserInfoUtil;
import com.dqyt.khjc.bean.HistoryBean;
import com.dqyt.khjc.bean.ReceiptTbBean;
import com.dqyt.khjc.bean.ResultMsg;
import com.dqyt.khjc.bean.User;
import com.dqyt.khjc.mapper.ReceiptTbMapper;
import com.dqyt.khjc.swicth.SwitchConfig;
import com.dqyt.khjc.utils.IpUtil;
import com.dqyt.khjc.utils.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class AppReceiptTbService {

    @Autowired
    private ReceiptTbMapper receiptTbMapper;


    /**
     * 分页查询 工作量确认单
     * @param request
     * @param page
     * @param size
     * @return
     */
    public ResultMsg getQrdListPage(HttpServletRequest request, String token, long userId, Integer page, Integer size){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_App(userId, token); // 验证用户是否登录
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值

            if (page != null && size != null) {
                page = (page - 1) * size;
            }
            //分页查询 工作量确认单
            List<ReceiptTbBean>  receiptTbBeans= receiptTbMapper.getQrdListPage(page,size);
            ret.setCode(ResultMsg.SUCESS);
            ret.setData(receiptTbBeans);
        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }




    /**
     * 设置 驳回工作量确认单
     * @param request
     * @return
     */
    public ResultMsg rejectQrdDal(HttpServletRequest request,String token, long userId,String rwdbh,String bhgyy){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_App(userId, token); // 验证用户是否登录
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值

            long res = receiptTbMapper.updateReceiptTbBytp2(rwdbh,bhgyy);

            if(res>0){
                ret.setCode(ResultMsg.SUCESS);
                ret.setMsg("操作成功");
                return ret;
            }else{
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("数据异常，请稍候再试");
                return ret;
            }

        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }



    /**
     * 设置 通工作量确认单
     * @param request
     * @return
     */
    public ResultMsg srtQrdOkProgress(HttpServletRequest request,String token, long userId,String rwdbh){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_App(userId, token); // 验证用户是否登录
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            User muser = UserInfoUtil.getUserInfologin(userId,token);
            ret = new ResultMsg(); // 声明一个返回值
            if(null == muser){
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("登录过期");
                return ret;
            }
            String qzrq = TimeUtil.formatTime_tow(TimeUtil.currentTimeMillis());
            long res = receiptTbMapper.srtQrdOkProgress2(rwdbh,muser.getUserId(),muser.getUserName(),qzrq);
            if(res>0){
                ret.setCode(ResultMsg.SUCESS);
                ret.setMsg("操作成功");
                return ret;
            }else{
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("数据异常，请稍候再试");
                return ret;
            }

        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }






}
